<?php


namespace app\common\helper;

use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Picqer\Barcode\BarcodeGeneratorDynamicHTML;
use Picqer\Barcode\BarcodeGeneratorPNG;

class QrcodeHelper
{
    protected $_qr;
    const WRITE_NAME = 'png';

    public function __construct() {
        // https://gitee.com/zoujingli/qrcode
    }

    /**
     * 生成二维码
     * @param $content //需要写入的内容
     */
    public function create($content) {
        $writer = new PngWriter();
        $qrCode = QrCode::create($content)
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize(300)
            ->setMargin(10)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        $result = $writer->write($qrCode);
        return $result->getDataUri();
    }

    public function barcode($content)
    {
        $generator = new BarcodeGeneratorPNG();
        return "data:image/png;base64," . base64_encode($generator->getBarcode($content,$generator::TYPE_CODE_128));
    }
}